#!/usr/bin/python
# -*- coding: utf-8 -*-
# author: yoshiii.yoshiii@googlemail.com
# you found this code on http://yoshiii-box.googlecode.com/svn/ 
#

from plotting_tools import *

# bstr_pos: only positive integers
# zero     -> ''
# negative -> ''
bstr_pos = lambda n: n>0 and bstr_pos(n>>1)+str(n&1) or ''

# bstr_nonneg: only non-negative integers
# zero     -> '0'
# negative -> ''
bstr_nonneg = lambda n: n>0 and bstr_nonneg(n>>1).lstrip('0')+str(n&1) or '0'

# bstr_sgn: all integers, signed
# zero -> '0'
# negative get a minus sign 
bstr_sgn = lambda n: n<0 and '-'+binarystr(-n) or n and bstr_sgn(n>>1).lstrip('0')+str(n&1) or '0'

# bstr: all integers
# zero -> '0'
# negative represented as complements, 16-bit by default
# optional second argument specifies number of bits 
bstr = lambda n, l=16: n<0 and binarystr((2L<<l)+n) or n and bstr(n>>1).lstrip('0')+str(n&1) or '0'

bin = bstr


from math import log10
def db2f(db):
    """dB => factor"""
    return 10**(db/10.0)

dbm2mw = db2f

def f2db(f):
    """convert factor to => dB"""
    return 10*log10(f)

mw2dbm = f2db


def frange(a, b, step=1):
    while a < b:
	yield a
	a += step

def picture_print_size(size=(587,880), dpi=150):
    """convert picture size in (x_pt, y_pt) into print size in (x_cm, y_cm) depending on given resolution
	returns pt / dpi * 2.45
    """
    return tuple(map(lambda x:float(x)/dpi*2.45, size))
